import sys

import cv2


class camera:
    def __init__(self) -> None:
        self.cap = cv2.VideoCapture(0)
        self.face_model = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

    def cameraRun(self):
        grayFace = []
        faces = 0
        ret, img = self.cap.read(0)
        if ret:
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            try:
                faces = self.face_model.detectMultiScale(gray, 1.1, 3, 0, (100, 100))
                # print(type(faces))
                if type(faces) != tuple:
                    for (x, y, w, h) in faces:
                        cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
                        grayFace = gray[y:y + h, x:x + w]
                    faces = 1
            except Exception as e:
                print(e)
        return img, grayFace, faces

    def cameraClose(self):
        self.cap.release()


# cam = camera()
# while True:
#     frame, GrayFace = cam.cameraRun()
#     if type(GrayFace) != list:
#         cv2.imshow('123', frame)
#         cv2.waitKey(50)
# cam.cameraClose()


